package com.ly.controller;

import com.ly.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RequestMapping("consumer")
@RestController
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderHystrixController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

    @GetMapping("/payment/hystrix/ok/{id}")
    public String paymentInfo_ok(@PathVariable("id") String id) {
        return paymentHystrixService.paymentInfo_ok(id);
    }

    @GetMapping("/payment/hystrix/false/{id}")
    public String getPaymentInfoFalse(@PathVariable("id") String id) {
        return paymentHystrixService.getPaymentInfoFalse(id);
    }


    @GetMapping("/payment/hystrix/timeout/{id}")
    /*@HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value="1500")
    })*/
    @HystrixCommand
    public String paymentInfo_TimeOut(@PathVariable("id") String id)
    {
        String result = paymentHystrixService.getPaymentInfoFalse(id);
        return result;
    }
    public String paymentTimeOutFallbackMethod(@PathVariable("id") String id)
    {
        return "我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
    }

    /**
     *但是目前会有一个问题，就是每个业务都对应一个服务降级的方法，就会导致代码膨胀，技术上是可以的，但是不建议这么设计
     *所以就必须设计统一的服务降级，通用的和独享的各自分开，避免了代码膨胀，合理的减少了代码量
     * 统一和自定义分开
     * 下面就是全局的fallback方法
     * 具体实现方案：
     *      1、只需要为Feign客户端定义的接口添加一个服务降级处理的实现类即可实现解耦
     *      2、注释掉自定义的注解
     *      3、加上新的注释@HystrixCommand
     */
    public String payment_Global_FallbackMethod() {
        return "Global异常处理信息，请稍后再试！";
    }

}
